package github.kawaiior.yuanchuxiuzhen.common.entity.addon.model;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import github.kawaiior.yuanchuxiuzhen.common.entity.addon.ArmyAntEntity;
import github.kawaiior.yuanchuxiuzhen.util.RenderUtil;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.util.math.MathHelper;

public class ArmyAntEntityModel extends EntityModel<ArmyAntEntity> {

    private final ModelRenderer head;
    private final ModelRenderer leftmo;
    private final ModelRenderer rightmo;
    private final ModelRenderer body;
    private final ModelRenderer pig;
    private final ModelRenderer rightarm1;
    private final ModelRenderer rightarm2;
    private final ModelRenderer rightarm3;
    private final ModelRenderer leftarm1;
    private final ModelRenderer leftarm2;
    private final ModelRenderer leftarm3;

    public ArmyAntEntityModel(){
        this.textureWidth = 48;
        this.textureHeight = 36;

        this.head = new ModelRenderer(this);
        this.head.setRotationPoint(0.0F, 18.0F, 0.0F);
        addBox(this.head, 0, 0, -4.0F, -4.0F, -4.0F, 8, 8, 4, 0.0F, false);

        this.leftmo = new ModelRenderer(this);
        this.leftmo.setRotationPoint(4.0F, 0.0F, -4.0F);
        RenderUtil.setRotationAngle(this.leftmo, 0.0F, 0.0F, 1.5708F);
        this.head.addChild(this.leftmo);
        addBox(this.leftmo, 0, 12, -4.0F, 0.0F, -4.0F, 7, 4, 4, 0.0F, false);
        this.rightmo = new ModelRenderer(this);
        this.rightmo.setRotationPoint(-4.0F, 0.0F, -4.0F);
        RenderUtil.setRotationAngle(this.rightmo, 0.0F, 0.0F, -1.5708F);
        this.head.addChild(this.rightmo);
        addBox(this.rightmo, 0, 12, -3.0F, 0.0F, -4.0F, 7, 4, 4, 0.0F, false);
        this.body = new ModelRenderer(this);
        this.body.setRotationPoint(0.0F, 19.0F, 3.0F);
        addBox(this.body, 24, 0, -3.0F, -3.0F, -3.0F, 6, 6, 6, 0.0F, false);
        this.pig = new ModelRenderer(this);
        this.pig.setRotationPoint(0.0F, 19.0F, 6.0F);
        addBox(this.pig, 0, 20, -4.0F, -5.0F, 0.0F, 8, 8, 8, 0.0F, false);
        this.rightarm1 = new ModelRenderer(this);
        this.rightarm1.setRotationPoint(-3.0F, 21.0F, 1.0F);
        RenderUtil.setRotationAngle(this.rightarm1, 0.0F, -0.6109F, -0.3491F);
        addBox(this.rightarm1, 15, 12, -8.0F, -1.0F, -1.0F, 8, 2, 2, 0.0F, false);
        this.rightarm2 = new ModelRenderer(this);
        this.rightarm2.setRotationPoint(-3.0F, 21.0F, 3.0F);
        RenderUtil.setRotationAngle(this.rightarm2, 0.0F, 0.0F, -0.3491F);
        addBox(this.rightarm2, 15, 12, -8.0F, -1.0F, -1.0F, 8, 2, 2, 0.0F, false);
        this.rightarm3 = new ModelRenderer(this);
        this.rightarm3.setRotationPoint(-3.0F, 21.0F, 5.0F);
        RenderUtil.setRotationAngle(this.rightarm3, 0.0F, 0.6109F, -0.3491F);
        addBox(this.rightarm3, 15, 12, -8.0F, -1.0F, -1.0F, 8, 2, 2, 0.0F, false);
        this.leftarm1 = new ModelRenderer(this);
        this.leftarm1.setRotationPoint(3.0F, 21.0F, 1.0F);
        RenderUtil.setRotationAngle(this.leftarm1, 0.0F, 0.6109F, 0.3491F);
        addBox(this.leftarm1, 15, 16, 0.0F, -1.0F, -1.0F, 8, 2, 2, 0.0F, false);
        this.leftarm2 = new ModelRenderer(this);
        this.leftarm2.setRotationPoint(3.0F, 21.0F, 3.0F);
        RenderUtil.setRotationAngle(this.leftarm2, 0.0F, 0.0F, 0.3491F);
        addBox(this.leftarm2, 15, 16, 0.0F, -1.0F, -1.0F, 8, 2, 2, 0.0F, false);
        this.leftarm3 = new ModelRenderer(this);
        this.leftarm3.setRotationPoint(3.0F, 21.0F, 5.0F);
        RenderUtil.setRotationAngle(this.leftarm3, 0.0F, -0.6109F, 0.3491F);
        addBox(this.leftarm3, 15, 16, 0.0F, -1.0F, -1.0F, 8, 2, 2, 0.0F, false);
    }

    private void addBox(ModelRenderer mb, int tx, int ty, float x, float y, float z, float width, float height, float depth, float delta, boolean mirrorIn){
        ModelRenderer mr = new ModelRenderer(this);
        mb.addChild(mr);
        mr.setTextureOffset(tx, ty).addBox(x,y,z,width,height,depth,delta,mirrorIn);
    }

    @Override
    public void setRotationAngles(ArmyAntEntity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
        this.head.rotateAngleY = netHeadYaw / 57.295776F;
        this.head.rotateAngleX = headPitch / 57.295776F;
        this.rightmo.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
        this.leftmo.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
        this.rightarm3.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
        this.rightarm2.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
        this.leftarm3.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
        this.rightarm1.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
        this.leftarm2.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * 1.0F * limbSwingAmount;
        this.leftarm1.rotateAngleX = MathHelper.cos(limbSwing * 1.0F) * -1.0F * limbSwingAmount;
    }

    @Override
    public void render(MatrixStack matrixStackIn, IVertexBuilder bufferIn, int packedLightIn, int packedOverlayIn, float red, float green, float blue, float alpha) {
        this.head.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.body.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.pig.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightarm1.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightarm2.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.rightarm3.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.leftarm1.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.leftarm2.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
        this.leftarm3.render(matrixStackIn, bufferIn, packedLightIn, packedOverlayIn);
    }
}
